/*
 * @Author: bor_fish 1219854025@qq.com
 * @Date: 2025-04-22 15:23:34
 * @LastEditors: bor_fish 1219854025@qq.com
 * @LastEditTime: 2025-04-26 20:56:15
 * @FilePath: /myCAN/Summarizer.h
 */
#pragma once
#include "Diagnostic_thread.h"
#include <atomic>
#include <thread>
#include <chrono>
#include <mutex>
#include <vector>

class Summarizer {
private:
    std::vector<DiagnosticThread*> diagnostics;  // 存储所有诊断线程的指针
    std::atomic<bool> running{true};
    std::thread summary_thread_;
    std::mutex stats_mutex_;
    
    // 统计信息结构
    struct BufferStats {
        size_t total_packets{0};
        size_t processed_packets{0};
        size_t buffer_usage{0};
    };
    BufferStats stats_;

public:
    // 构造函数，接收多个诊断线程
    Summarizer(std::initializer_list<DiagnosticThread*> diags) : diagnostics(diags) {}
    void summarizeLoop();
    void stop() { 
        running = false;
        if(summary_thread_.joinable()) {
            summary_thread_.join();
        }
    }
    void start() {
        summary_thread_ = std::thread(&Summarizer::summarizeLoop, this);
    }
    
    // 对外调用接口
    // 获取统计信息
    BufferStats getStats() {
        std::lock_guard<std::mutex> lock(stats_mutex_);
        return stats_;
    }
};